﻿using System;
using System.IO;
using System.Text;

namespace ACLibs
{
    /// <summary>
    /// 程序LOG日志本地输出
    /// </summary>
    public static class LogHelper
    {
        #region 日志配置
        public static class Config
        {
            /// <summary>
            /// 设置或获取日志文件名称
            /// 默认：ACLog.log
            /// </summary>
            public static string LogName
            {
                set { _LogName = value; }
                get
                {
                    //检测是否有日志文件名称
                    if (string.IsNullOrWhiteSpace(_LogName))
                    {
                        return "Logs.log";
                    }
                    else
                    {
                        return _LogName;
                    }
                }

            }
            private static string _LogName;

            //日志路径
            public static string LogPath { get { return Path.Combine(System.Environment.CurrentDirectory, LogName); } }
            //日志备份路径
            public static string LogPathBack { get { return Path.Combine(System.Environment.CurrentDirectory, LogName + ".back"); } }
        }
        #endregion
        #region 变量定义
        

        

        #endregion

        /// <summary>
        /// 设置日志为新日志文件
        /// </summary>
        public static void SetNewLogFile()
        {
            //检测是否存在日志文件
            if (File.Exists(Config.LogPath))
            {
                //检测是否有备份文件
                if (File.Exists(Config.LogPathBack))
                {
                    //存在就删除,并把现在的日志文件改名为备份文件
                    File.Delete(Config.LogPathBack);

                }
                else
                {
                    //不存在就直接改名
                    File.Move(Config.LogPath, Config.LogPathBack);
                }
            }
        }
        /// <summary>
        /// 日志文件流对象
        /// </summary>
        private static StreamWriter streamWriter = null;


        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="ModuleName">当前模块对象,一般写this</param>
        /// <param name="logType">日志类型</param>
        /// <param name="logs">日志内容</param>
        public static void Log(object ModuleName, LogType logType, string logs)
        {
            //读取日志流对象
            streamWriter = File.AppendText(Config.LogPath);

            //构建日志内容
            //2020-07-07 18:46:55.222 [模块.名字] [一般] 日志内容;
            StringBuilder logBuild = new StringBuilder();
            logBuild.Append(DateTime.Now).Append(" ");
            logBuild.Append("[").Append(ModuleName.GetType().FullName).Append("] ");
            logBuild.Append("[").Append(logType.ToString()).Append("] ");
            logBuild.Append(logs).Append(";");
            //throw new Exception(logBuild.ToString());

            streamWriter.WriteLine(logBuild.ToString());
            streamWriter.Flush(); //写入数据
            streamWriter.Close(); //关闭
        }

        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="errObject">错误对象</param>
        public static void Log(Exception errObject)
        {
            Log(errObject, LogType.错误, errObject.Message);
        }

        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="logType">日志类型</param>
        /// <param name="logs">日志内容</param>
        public static void Log(LogType logType, string logs)
        {
            Log(new Exception("日志", new Exception("无")), logType, logs);
        }

        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="logs">日志内容</param>
        public static void Log(string logs)
        {
            Log(new Exception("日志", new Exception("无")), LogType.一般, logs);
        }

        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="logType">日志类型</param>
        /// <param name="dateTime">日志时间</param>
        /// <param name="logs">日志内容</param>
        public static void Log(LogType logType, DateTime dateTime, string logs)
        {
            //创建日志文件
            string logPath = Path.Combine(System.Environment.CurrentDirectory, Config.LogName);

            if (!File.Exists(logPath))
            {
                File.Create(logPath).Close();
            }
            //构建日志内容
            string tmpLogs = string.Format("{0}:[{1}]:{2}", dateTime.ToString(), logType.ToString(), logs);
            //写入日志到文件
            StreamWriter streamWriter = File.AppendText(logPath);
            streamWriter.WriteLine(tmpLogs);
            streamWriter.Flush();
            streamWriter.Close();
        }

        /// <summary>
        /// 日志类型
        /// </summary>
        public enum LogType
        {
            一般,
            警告,
            错误
        }
    }
}
